﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using GPSTrackingWebsiteManager.ClassManager;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class GPSGroups : ClassManager.SessionManagerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = "GPSGroupIdSource.Name";
                expression.SetSortOrder("Ascending");
                this.GPSGroupsGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
            }
        }

        protected void GPSGroupsGrid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();
            try
            {
                SessionManager objSessionManager = new SessionManager();
                objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];

                tm.BeginTransaction();

                GPSTrackingDBV2.Entities.GpsGroups GpsGroupsEntity = ClassManager.InvokeEntityManager.Invoke_GpsGroups_Entity(e);

                GpsGroupsEntity.GpsGroupId = GPSTrackingDBV2Custom.Data.DataRepository.GPSGroupsProvider.GetNextID();

                GPSTrackingDBV2.Data.DataRepository.GpsGroupsProvider.Insert(tm, GpsGroupsEntity);

                GPSTrackingDBV2.Entities.CompaniesByGpsGroups CompaniesByGpsGroupsEntity = new GPSTrackingDBV2.Entities.CompaniesByGpsGroups();

                CompaniesByGpsGroupsEntity.CompanyId = objSessionManager.CompanyIDSelectedComboBox;
                CompaniesByGpsGroupsEntity.GpsGroupId = GpsGroupsEntity.GpsGroupId;

                GPSTrackingDBV2.Data.DataRepository.CompaniesByGpsGroupsProvider.Insert(tm, CompaniesByGpsGroupsEntity);

                tm.Commit();
            }
            catch (Exception ex)
            {
                tm.Rollback();
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede insertar la información.");
            }
        }

        protected void GPSGroupsGrid_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {

                GPSTrackingDBV2.Entities.GpsGroups GPSGroupsEntity = ClassManager.InvokeEntityManager.Invoke_GpsGroups_Entity(e);
                GPSGroupsEntity.OriginalGpsGroupId = GPSGroupsEntity.GpsGroupId;

                GPSTrackingDBV2.Data.DataRepository.GpsGroupsProvider.Update(GPSGroupsEntity);

            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede actualizar la información.");
            }
        }

        protected void GPSGroupsGrid_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();
            try
            {
                SessionManager objSessionManager = new SessionManager();
                objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];

                tm.BeginTransaction();

                GPSTrackingDBV2.Entities.GpsGroups GPSGroupsEntity = ClassManager.InvokeEntityManager.Invoke_GpsGroups_Entity(e);

                GPSTrackingDBV2.Data.DataRepository.CompaniesByGpsGroupsProvider.Delete(tm, objSessionManager.CompanyIDSelectedComboBox, GPSGroupsEntity.GpsGroupId);

                GPSTrackingDBV2.Data.DataRepository.GpsGroupsProvider.Delete(tm, GPSGroupsEntity.GpsGroupId);

                tm.Commit();
            }
            catch (Exception ex)
            {
                tm.Rollback();
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede eliminar el registro porque el grupo se encuentra asociados a otros registros.");
            } 
        }        

        protected void GPSGroupsGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            try
            {
                SessionManager objSessionManager = new SessionManager();
                objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];

                getGPSGroupsByCompanyID(objSessionManager.CompanyIDSelectedComboBox);
                GPSGroupsGrid.Height = Unit.Parse(HiddenFieldHeight.Value.ToString());
            }
            catch (Exception ex) {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);                
            }
        }
        
        protected void getGPSGroupsByCompanyID(int companyID)
        {
            try
            {
                GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.CompaniesByGpsGroups> CompaniesByGpsGroupsEntity = GPSTrackingDBV2.Data.DataRepository.CompaniesByGpsGroupsProvider.GetByCompanyId(companyID);
                GPSTrackingDBV2.Data.DataRepository.CompaniesByGpsGroupsProvider.DeepLoad(CompaniesByGpsGroupsEntity, true, GPSTrackingDBV2.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingDBV2.Entities.GpsGroups));
                GPSGroupsGrid.DataSource = CompaniesByGpsGroupsEntity;                
            }
            catch (Exception ex) {
                throw ex;
            }
        }

        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_miniError", "objWindowsMessageManager.show_miniError('" + msg + "','Grupos de dispositivos ...');", true);
        }

        protected void lbClearFilters_Click(object sender, EventArgs e)
        {
            GPSGroupsGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in GPSGroupsGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }                
            }


            GPSGroupsGrid.MasterTableView.Rebind();
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }

        
    }
}